<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable>
          <el-option
            v-for="dict in dict.type.order_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="游戏" prop="gameId">
        <el-select v-model="queryParams.gameId" placeholder="请选择游戏" clearable>
          <el-option
            v-for="game in gameList"
            :key="game.id"
            :label="game.name"
            :value="game.id">
          </el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="任务Id" prop="rwId">-->
<!--        <el-input-->
<!--          v-model="queryParams.rwId"-->
<!--          placeholder="请输入任务Id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="区服类型" prop="qfType">-->
<!--        <el-select v-model="queryParams.qfType" placeholder="请选择区服类型" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.game_qf_type"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="区服Id" prop="qfId">-->
<!--        <el-input-->
<!--          v-model="queryParams.qfId"-->
<!--          placeholder="请输入区服Id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="用户昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="是否需要担保人" prop="needDbr">-->
<!--        <el-select v-model="queryParams.needDbr" placeholder="请选择是否需要担保人" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.business_yes_no"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable>
          <el-option
            v-for="dict in dict.type.order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="上架 下架" prop="status">-->
<!--        <el-select v-model="queryParams.status" placeholder="请选择上架 下架" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.business_yes_no"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->

      <el-form-item label="订单标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入订单标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['business:order:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['business:order:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['business:order:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['business:order:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="订单标题" align="center" prop="title" show-overflow-tooltip  />
<!--      <el-table-column label="订单编号" align="center" prop="orderNo" />-->
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="游戏" align="center" prop="gameId" >
        <template slot-scope="scope">
          <span>{{ scope.row.game.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务" align="center" prop="rwName" />
      <el-table-column label="任务补充" align="center" prop="rwBc"   show-overflow-tooltip />
      <el-table-column label="区服类型" align="center" prop="qfType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.game_qf_type" :value="scope.row.qfType"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="区服Id" align="center" prop="qfId" />-->
      <el-table-column label="用户" align="center" prop="userId" >
        <template slot-scope="scope">
          <span>{{ scope.row.user.nickName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="代练师" align="center" prop="playUserId" >
        <template slot-scope="scope">
          <span>{{ scope.row.playUser  && scope.row.playUser.nickName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否需要担保人" align="center" prop="needDbr">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.business_yes_no" :value="scope.row.needDbr"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="用户优惠券Id" align="center" prop="userCouponId" />-->
<!--      <el-table-column label="优惠券Id" align="center" prop="couponId" />-->
<!--      <el-table-column label="优惠券金额" align="center" prop="couponPrice" />-->
<!--      <el-table-column label="实付金额" align="center" prop="price" />-->
      <el-table-column label="订单金额" align="center" prop="orderPrice" />
      <el-table-column label="保证金" align="center" prop="bzPrice" />
      <el-table-column label="时限要求" align="center" prop="hour" />
      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_status" :value="scope.row.orderStatus"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="阶段 5 进入仲裁阶段   6 强制仲裁" align="center" prop="jdStatus" />-->
<!--      <el-table-column label="仲裁费用支付方式  1败方支付 2双方AA" align="center" prop="zcType" />-->
      <el-table-column label="是否上架" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.business_yes_no" :value="scope.row.status"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="完成时间" align="center" prop="finallyTime" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.finallyTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->

      <el-table-column label="时间" align="center" prop="createTime" ></el-table-column>
<!--      <el-table-column label="备注" align="center" prop="remark" />-->

      <el-table-column label="操作" align="center"  fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:order:edit']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="gthandleUpdate(scope.row)"
            v-hasPermi="['business:order:edit']"
          >沟通记录</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:order:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
<el-form ref="form" :model="form" :rules="rules" label-width="120px">
  <el-row :gutter="20">
    <el-col :span="12">
      <el-form-item label="订单标题" prop="title">
        <el-input v-model="form.title" placeholder="请输入订单标题" />
      </el-form-item>
    </el-col>
    <el-col :span="12">
      <el-form-item label="类型" prop="type">
        <el-select v-model="form.type" placeholder="请选择类型">
          <el-option
            v-for="dict in dict.type.order_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-col>
  </el-row>

  <el-row :gutter="20">
    <el-col :span="12">
      <el-form-item label="游戏" prop="gameId">
        <el-input v-model="form.game && form.game.name" placeholder="请输入游戏" />
      </el-form-item>
    </el-col>
    <el-col :span="12">
      <el-form-item label="任务" prop="rwName">
        <el-input v-model="form.rwName" placeholder="请输入任务" />
      </el-form-item>
    </el-col>
  </el-row>

  <el-row :gutter="20">
    <el-col :span="12">
      <el-form-item label="任务补充" prop="rwBc">
        <el-input v-model="form.rwBc" placeholder="请输入任务补充" />
      </el-form-item>
    </el-col>
    <el-col :span="12">
      <el-form-item label="区服类型" prop="qfType">
        <el-select v-model="form.qfType" placeholder="请选择区服类型">
          <el-option
            v-for="dict in dict.type.game_qf_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-col>
  </el-row>

  <el-row :gutter="20">
    <el-col :span="12">
      <el-form-item label="区服" prop="qfName">
        <el-input v-model="form.qfName" placeholder="请输入区服" />
      </el-form-item>
    </el-col>
    <el-col :span="12">
      <el-form-item label="是否需要担保人" prop="needDbr">
        <el-select v-model="form.needDbr" placeholder="请选择是否需要担保人">
          <el-option
            v-for="dict in dict.type.business_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-col>
  </el-row>

  <el-row :gutter="20">
    <el-col :span="12">
      <el-form-item label="优惠券金额" prop="couponPrice">
        <el-input v-model="form.couponPrice" placeholder="请输入优惠券金额" />
      </el-form-item>
    </el-col>
    <el-col :span="12">
      <el-form-item label="订单金额" prop="orderPrice">
        <el-input v-model="form.orderPrice" placeholder="请输入订单金额" />
      </el-form-item>
    </el-col>
  </el-row>

  <el-row :gutter="20">
    <el-col :span="12">
      <el-form-item label="保证金" prop="bzPrice">
        <el-input v-model="form.bzPrice" placeholder="请输入保证金" />
      </el-form-item>
    </el-col>
    <el-col :span="12">
      <el-form-item label="时限要求" prop="hour">
        <el-input v-model="form.hour" placeholder="请输入时限要求" />
      </el-form-item>
    </el-col>
  </el-row>

  <el-row :gutter="20">
    <el-col :span="12">
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="form.orderStatus" placeholder="请选择订单状态">
          <el-option
            v-for="dict in dict.type.order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-col>
    <el-col :span="12">
      <el-form-item label="是否上架" prop="status">
        <el-select v-model="form.status" placeholder="请选择是否上架">
          <el-option
            v-for="dict in dict.type.business_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-col>
  </el-row>

  <el-row :gutter="20">
    <el-col :span="12">
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" placeholder="请输入备注" />
      </el-form-item>
    </el-col>
    <el-col :span="12">
      <el-form-item label="用户昵称" prop="userId">
        <el-input v-model="form.user && form.user.nickName" placeholder="请输入用户昵称" />
      </el-form-item>
    </el-col>
  </el-row>

  <el-row :gutter="20">
    <el-col :span="12">
      <el-form-item label="代练师" prop="playUserId">
        <el-input v-model="form.playUser && form.playUser.nickName" placeholder="请输入代练师昵称" />
      </el-form-item>
    </el-col>
  </el-row>
</el-form>
      <el-divider content-position="center">担保人信息</el-divider>
      <OrderDbrRecord v-if="open" :order-id="form.id"></OrderDbrRecord>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="gtopen" width="1200px" append-to-body>
         <Ordernotice v-if="gtopen" :order-id="form.id"></Ordernotice>
          <el-button @click="gtcancel">取 消</el-button>
    </el-dialog>

  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/business/order";
import {listAllGame} from "@/api/business/game";
import OrderDbrRecord from "@/components/OrderDbrRecord/index.vue";
import Ordernotice   from "@/components/Ordernotice/index.vue";
export default {
  name: "Order",
  dicts: ['business_yes_no', 'game_qf_type', 'order_status', 'order_type'],
  components: { OrderDbrRecord ,Ordernotice},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      gtopen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: null,
        type: null,
        gameId: null,
        rwId: null,
        rwBc: null,
        qfType: null,
        qfId: null,
        userId: null,
        playUserId: null,
        needDbr: null,
        userCouponId: null,
        couponId: null,
        couponPrice: null,
        price: null,
        orderPrice: null,
        bzPrice: null,
        hour: null,
        orderStatus: null,
        jdStatus: null,
        zcType: null,
        status: null,
        orderTime: null,
        finallyTime: null,
        cancelTime: null,
        payTime: null,
        isPayed: null,
        payType: null,
        refundStatus: null,
        isComment: null,
        isRefund: null,
        closeType: null,
        title: null,
        outTradeNo: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      gameList:[]
    };
  },
  created() {
    this.getList();
    this.getGameList()
  },
  methods: {

    /** 查询游戏列表 */
    getGameList() {
      listAllGame().then(response => {
        this.gameList = response.data;
      });
    },
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    gtcancel() {
      this.gtopen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNo: null,
        type: null,
        gameId: null,
        rwId: null,
        rwBc: null,
        qfType: null,
        qfId: null,
        userId: null,
        playUserId: null,
        needDbr: null,
        userCouponId: null,
        couponId: null,
        couponPrice: null,
        price: null,
        orderPrice: null,
        bzPrice: null,
        hour: null,
        orderStatus: null,
        jdStatus: null,
        zcType: null,
        status: null,
        orderTime: null,
        finallyTime: null,
        cancelTime: null,
        payTime: null,
        isPayed: null,
        payType: null,
        refundStatus: null,
        isComment: null,
        isRefund: null,
        closeType: null,
        title: null,
        remark: null,
        outTradeNo: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单";
      });
    },
    gthandleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.form.id=id;
      this.gtopen = true;
      this.title = "沟通记录";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单编号为"' + ids + '"的数据项？').then(function() {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
